package com.pkq.hashcode;

import lombok.Getter;
import lombok.Setter;

import java.util.HashSet;
import java.util.Objects;


public class Student {
    public static void main(String[] args) {
        Stu stu = new Stu();
        stu.setName("name");
        Stu stu1 = new Stu();
        stu1.setName("name");

        System.out.println(stu == stu1);
        System.out.println(stu.equals(stu1));

        Stu.StuNoOverride stuNoOverride = new Stu.StuNoOverride();
        stuNoOverride.setName("s");
        Stu.StuNoOverride stuNoOverride2 = new Stu.StuNoOverride();
        stuNoOverride2.setName("s");

        HashSet<Object> hashSet = new HashSet<>();
        hashSet.add(stu);
        hashSet.add(stu1);
        hashSet.add(stuNoOverride);
        hashSet.add(stuNoOverride2);
        hashSet.stream().forEach(System.out::println);
    }
}

@Setter
@Getter
class Stu {
    private String name;
    private String age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Stu stu = (Stu) o;
        return Objects.equals(name, stu.name) && Objects.equals(age, stu.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }


    @Setter
    @Getter
    static class StuNoOverride {
        private String name;
        private String age;
    }
}
